package cn.thoughtworks.school.controllers;

import cn.thoughtworks.school.annotations.Auth;
import cn.thoughtworks.school.entities.BasicQuizSubmission;
import cn.thoughtworks.school.entities.QuizSuggestion;
import cn.thoughtworks.school.entities.User;
import cn.thoughtworks.school.entities.dto.AssignmentQuizData;
import cn.thoughtworks.school.handlers.BusinessException;
import cn.thoughtworks.school.repositories.BasicQuizSubmissionRepository;
import cn.thoughtworks.school.services.QuizService;
import cn.thoughtworks.school.services.QuizSuggestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/api/v3/quizzes")
public class QuizController {
    @Autowired
    private QuizSuggestionService quizSuggestionService;
    @Autowired
    private BasicQuizSubmissionRepository basicQuizSubmissionRepository;
    @Autowired
    private QuizService quizService;

    @RequestMapping(value = "/{quizIds}/assignment/{assignmentIds}", method = RequestMethod.GET)
    public ResponseEntity getAssignmentQuizzes(@PathVariable String quizIds, @PathVariable String assignmentIds) {
        List<BasicQuizSubmission> basicQuizSubmissionList = new ArrayList<>();
        String[] quizIdList = quizIds.split(",");
        String[] assignmentIdList = assignmentIds.split(",");
        for (int i = 0; i < quizIdList.length; i++) {
            List<BasicQuizSubmission> basicQuizSubmissions = basicQuizSubmissionRepository.findByQuizIdAndAssignmentIdOrderBySubmitTimeDesc(Long.parseLong(quizIdList[i]), Long.parseLong(assignmentIdList[i]));
            if (basicQuizSubmissions.size() != 0) {
                basicQuizSubmissionList.add(basicQuizSubmissions.get(0));
            }
        }
        return new ResponseEntity<>(basicQuizSubmissionList, HttpStatus.OK);
    }

    @PostMapping("duplication")
    public ResponseEntity copyQuizzes(@RequestBody List<AssignmentQuizData> assignmentQuizData) {
      List<AssignmentQuizData> updatedAssignmentQuizData = quizService.copyQuizzes(assignmentQuizData);

      return new ResponseEntity(updatedAssignmentQuizData, HttpStatus.CREATED);
    }

    @PostMapping("suggestions")
    public ResponseEntity submitQuizSuggestion(@RequestBody QuizSuggestion quizSuggestion) {
      quizSuggestionService.submitQuizSuggestion(quizSuggestion);

      return new ResponseEntity(HttpStatus.CREATED);
    }

    @GetMapping("suggestions")
    public ResponseEntity getQuizSuggestion(@Auth User user) {
        return ResponseEntity.ok(quizSuggestionService.getQuizSuggestionByOrganization(user));
    }

    @PutMapping("suggestions/{id}")
    public ResponseEntity markAsSolved(@PathVariable Long id) throws BusinessException {
        quizSuggestionService.markAsSolvedById(id);
        return ResponseEntity.noContent().build();
    }

}
